
// 想要使用必须先引入 defineStore；
import { defineStore } from 'pinia';
import Cookies from 'js-cookie'
import { getUserMenu } from '@/api/system/user';
import router from "@/router/index"
const cookiesStorage: any = {
    setItem(key: any, state: any): any {
        let myState = JSON.parse(state)
        return Cookies.set(key, myState[key], { expires: 3 })
    },
    getItem(key: any): string {
        return JSON.stringify({
            [key]: Cookies.get(key),
        })
    },
    // removeItem(key) { },
    // clear() { },
    // key(key): any { },
    // length,
}
interface stateModele {
    token?: number | string,
    list: any,
    isCollapse: boolean,
    routerList: any,
    visitor: any
}
export const GetRouter = defineStore('routerList', {
    state: (): stateModele => {  // 存放的就是模块的变量
        return {
            token: "",
            list: [],
            isCollapse: false,
            routerList: [],
            visitor: ""
        }
    },
    persist: {
        enabled: true,
        strategies: [
            {
                storage: window.localStorage,
                key: 'isCollapse',
                paths: ['isCollapse']
            },
            {
                storage: window.localStorage,
                key: 'token',
                paths: ['token']
            },
            {
                storage: window.localStorage,
                key: 'visitor',
                paths: ['visitor']
            },
        ],
    },
    getters: {},
    actions: {
        async deleteToken(data: any) {
            this.token = data
        },
        async getToken(data: any) {
            this.token = data
            // this.SetRouterList()
        },
        SetRouter(data: any) {
            this.list = data
        },

        SetVisitor() {
            this.visitor = 'visitor'
        },
        DelVisitor() {
            this.visitor = ''
        },

        async SetRouterList() {
            const dt = await getUserMenu({ token: this.token })
            return dt
        },
    }
})
